package org.learn.intermediate.com.exercise;

import java.util.ArrayList;
import java.util.Comparator;

public class Exercise05 {
    public static void main(String[] args) {
        ArrayList<Employee01> employees = new ArrayList<>();
        employees.add(new Employee01("jack",12345,new MyDate(2000,12,12)));
        employees.add(new Employee01("tom",10000,new MyDate(2000,12,13)));
        employees.add(new Employee01("smith",20000,new MyDate(2000,12,14)));

        System.out.println(employees);
        System.out.println("======对员工进行排序=====");
        employees.sort(new Comparator<Employee01>() {
            @Override
            public int compare(Employee01 o1, Employee01 o2) {
                //验证参数
                if(!(o1 instanceof Employee01 && o2 instanceof Employee01)){
                    return 0;
                }
                //比较名字
                int i = o1.getName().compareTo(o2.getName());
                if(i != 0){
                    return i;
                }
                //
                int yearMinus = o1.getBirthday().getYear() - o2.getBirthday().getYear();
                if(yearMinus != 0){
                    return yearMinus;
                }
                //
                int monthMinus = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
                if (monthMinus != 0){
                    return monthMinus;
                }
                //
                return o1.getBirthday().getDay() - o2.getBirthday().getDay();
            }
        });
        System.out.println(employees);

    }
}
class Employee01{
    private String name;
    private double sal ;
    private MyDate birthday ;

    public Employee01(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return  "Employee01{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }
}
class MyDate{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}